#include "stdafx.h"
#include "questiondetaildialog.h"

QuestionDetailDialog::QuestionDetailDialog( QWidget * parent )
:   DialogBase( parent )
{
    m_question.value = 1;
    init();
}

QuestionDetailDialog::QuestionDetailDialog( QWidget * parent, const Question & question )
:   DialogBase( parent ),
    m_questionOld( question ),
    m_question( question )
{
    init();
    ui.questionPlainTextEdit->setPlainText( question.question );
    ui.answerPlainTextEdit->setPlainText( question.answer );
    ui.valueSpinBox->setValue( question.value );
}

QuestionDetailDialog::~QuestionDetailDialog()
{
}

void QuestionDetailDialog::init()
{
    connect( ui.questionPlainTextEdit, SIGNAL( textChanged() ), this, SLOT( onQuestionTextChanged() ) );
    connect( ui.answerPlainTextEdit, SIGNAL( textChanged() ), this, SLOT( onAnswerTextChanged() ) );
    connect( ui.valueSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( onValueSpinboxChanged( int ) ) );
    connect( ui.okPushButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
    connect( ui.cancelPushButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
}

void QuestionDetailDialog::enableSavePushButton()
{
    bool saveEnabled = (
        ! m_question.question.isEmpty() &&
        ! m_question.answer.isEmpty() &&
        m_question != m_questionOld );

    ui.okPushButton->setEnabled( saveEnabled );
}

const Question & QuestionDetailDialog::question() const
{
    return m_question;
}

void QuestionDetailDialog::onQuestionTextChanged()
{
    m_question.question = ui.questionPlainTextEdit->toPlainText();
    enableSavePushButton();
}

void QuestionDetailDialog::onAnswerTextChanged()
{
    m_question.answer = ui.answerPlainTextEdit->toPlainText();
    enableSavePushButton();
}

void QuestionDetailDialog::onValueSpinboxChanged( int value )
{
    m_question.value = value;
    enableSavePushButton();
}